#-*- coding: utf-8 -*- 
#  @author: hzq <151680523@qq.com>

# @version     $Id:filename.php

__author__ = "Administrator"
__date__ = "$2011-10-13 16:19:34$"
import os.path

import cPickle as cP
import libs
from pymmseg import mmseg
 
mmseg.dict_load_defaults()    

class tags:

    def __init__(self, dictDir):
        self.dictDir = dictDir
        self.dict = []
 
        self.fenci = []


    def _initDict(self, dict):
        try:
            filename = os.path.normpath("%s/%s.dict" % (self.dictDir, dict))
            f = file(filename, 'rb')
            self.dict = cP.load(f)
            f.close()
        except:
            filename = os.path.normpath("%s/%s.txt" % (self.dictDir, dict))
            f = file(filename, 'r')
            self.dict = f.readline().split('|')
            
            f.close()
            filename = os.path.normpath("%s/%s.dict" % (self.dictDir, dict))
            f = open(filename, "wb")
            cP.dump(self.dict, f, True)
            f.close()
    def do_fenci(self, strings):
        strings=str(strings)
        
        self.fenci = mmseg.Algorithm(strings)
#        for tok in algor:
#            print tok.text
      
#        fencis = str(libs.post(self.server, {'data':strings.decode('utf-8', 'ignore').encode('gbk')}))
#        fencis = fencis.replace('data=', '')
#        self.fenci = fencis.decode('gbk', 'ignore').encode('utf-8').split(' ')
       
          
            
       
        
        

    def getTags(self, dict):
        self._initDict(dict)
        
        tags = []
        tagss=[]
        for v in self.fenci:
            
            if v.text in self.dict:
                tags.append(v.text)
        for v1 in tags:
            if v1 not in tagss:
                tagss.insert(tags.count(v1),v1)
                
                
        return tagss






        






if __name__ == "__main__":
    t = tags('E:/web/shop/work/foods/spider/data/dict')
    t.do_fenci('猪肉东方红牛肉面岁的范师傅蜜蜂是个很蜂蜜面条呢姐啊首发白菜as将大幅回调')
    

    print t.getTags('food')
    
